package com.example.demo.controller;

import com.example.demo.config.AjaxResult;
import com.example.demo.model.CommentInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.CommentService;
import com.example.demo.util.ConstVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {
    
    @Autowired
    private CommentService commentService;

    @PostMapping("/buyer/submit")
    public AjaxResult submitBuyerComment(@RequestBody Map<String, Object> params, HttpSession session) {
        try {
            UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
            if (userInfo == null) {
                return AjaxResult.fail("请先登录");
            }

            String orderNo = (String) params.get("orderNo");
            String content = (String) params.get("content");
            Integer rating = (Integer) params.get("rating");

            if (orderNo == null || content == null || rating == null) {
                return AjaxResult.fail("参数不完整");
            }

            boolean success = commentService.submitBuyerComment(orderNo, userInfo.getId(), content, rating);
            return success ? AjaxResult.success("评价成功") : AjaxResult.fail("评价失败");
        } catch (Exception e) {
            return AjaxResult.fail("评价失败：" + e.getMessage());
        }
    }

    @PostMapping("/seller/submit")
    public AjaxResult submitSellerComment(@RequestBody Map<String, Object> params, HttpSession session) {
        try {
            UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
            if (userInfo == null) {
                return AjaxResult.fail("请先登录");
            }

            String orderNo = (String) params.get("orderNo");
            String content = (String) params.get("content");
            Integer rating = (Integer) params.get("rating");

            if (orderNo == null || content == null || rating == null) {
                return AjaxResult.fail("参数不完整");
            }

            boolean success = commentService.submitSellerComment(orderNo, userInfo.getId(), content, rating);
            return success ? AjaxResult.success("评价成功") : AjaxResult.fail("评价失败");
        } catch (Exception e) {
            return AjaxResult.fail("评价失败：" + e.getMessage());
        }
    }

    @GetMapping("/get/{orderNo}")
    public AjaxResult getComment(@PathVariable String orderNo, HttpSession session) {
        try {
            UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
            if (userInfo == null) {
                return AjaxResult.fail("请先登录");
            }

            CommentInfo comment = commentService.getComment(orderNo, userInfo.getId());
            if (comment == null) {
                return AjaxResult.fail("评论不存在或无权限查看");
            }

            return AjaxResult.success(comment);
        } catch (Exception e) {
            return AjaxResult.fail("获取评论失败：" + e.getMessage());
        }
    }
}
